﻿Public Class frmMkBill
    Dim daMkBill As OleDb.OleDbDataAdapter
    Dim dsMkBill As DataSet
    Dim Sql As String

    Private Sub frmMkBill_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        txtAmount.Text = gAmountOnBill
        txtAmount.Text = Format(CInt(txtAmount.Text), "#,##0")
        txtSeqs.Text = gSeqsOnBill
        txtCompCd.Text = gCompCdOnBill
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Call saveMkBill()
    End Sub
#Region "프로시져"
    '## 거래명세서 저장
    Private Sub saveMkBill()

        Call dbConnection()
        Dim oleCmd As OleDb.OleDbCommand
        Dim oleTran As OleDb.OleDbTransaction
        Dim rtnMsg As String = ""
        Dim valIncCd, valArticle, valBillMemo, valAmount, valOrgAmount, valTax, valBillCd, valCompCd As String
        Dim valAccountCd As String = "18"
        Dim curDt As DateTime = Now
        Dim valIncomeDt As DateTime = Format(Now, "yyyy-MM-dd")

        valArticle = txtIncome.Text
        valBillMemo = txtRemarks.Text
        valAmount = txtAmount.Text
        valCompCd = txtCompCd.Text

        If valArticle = "" Then
            dbConn.Close()
            MsgBox("내역을 바르게 입력해 주세요")
            txtIncome.Focus()
            Exit Sub
        End If

        valTax = (CDbl(txtAmount.Text) / 11)
        valOrgAmount = CDbl(txtAmount.Text) - CDbl(valTax)

        valBillCd = fnGetBillCd()
        valIncCd = gFnGetIncCd()
        '## 거래명세 코드 저장
        Sql = "UPDATE tblPrdInOut SET billCd='" + valBillCd + "' WHERE seq IN (" + txtSeqs.Text + ")"
        Call gSubSaveAction(Sql, "")

        Sql = "INSERT INTO tblIncome(incCd, compCd, article, incomeDt, accountCd, amount, orgAmount, tax, restAmount, billCd, billMemo, flag, addDt) " + _
                "   VALUES('" + valIncCd + "','" + valCompCd + "','" + valArticle + "','" + valIncomeDt + "','" + valAccountCd + "','" + valAmount + "','" + valOrgAmount + "','" + valTax + "','" + valAmount + "','" + valBillCd + "','" + valBillMemo + "','Y','" + curDt + "')"
        rtnMsg = "거래명세서가 매출에 등록되었습니다"


        oleCmd = New OleDb.OleDbCommand(Sql, dbConn)

        oleTran = dbConn.BeginTransaction
        oleCmd.Transaction = oleTran
        Try

            oleCmd.ExecuteNonQuery()
            oleTran.Commit()

            dbConn.Close()
            MsgBox(rtnMsg)
            Me.Close()
        Catch err As OleDb.OleDbException
            oleTran.Rollback()
            dbConn.Close()
            MsgBox(Sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub

    '# 거래명세서 코드 구하기
    Public Function fnGetBillCd() As String

        Dim maxBillCd As String
        Sql = "SELECT max(billCd)+1 as maxBillCd FROM tblPrdInOut"
        dsMkBill = New DataSet
        daMkBill = New OleDb.OleDbDataAdapter(Sql, dbConn)
        daMkBill.Fill(dsMkBill, "MxBillCd")

        maxBillCd = dsMkBill.Tables("MxBillCd").Rows(0).Item(0)

        Return maxBillCd
    End Function
#End Region

End Class